# Maintainer: umarcor <unai.martinezcorral@ehu.eus>

_realname=nextpnr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.7
pkgrel=4
pkgdesc="Portable FPGA place and route tool (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/YosysHQ/nextpnr"
license=('spdx:ISC')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-boost-libs"
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-omp"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-qt5-base"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-boost"
  "${MINGW_PACKAGE_PREFIX}-eigen3"
  "${MINGW_PACKAGE_PREFIX}-icestorm"
  "${MINGW_PACKAGE_PREFIX}-prjtrellis"
  "git"
)
source=("${_realname}::git+https://github.com/YosysHQ/${_realname}.git#tag=${_realname}-${pkgver}"
        "001-fix-find-boost-built-with-cmake.patch"
        "002-fix-build-with-boost-1.85.patch::https://github.com/YosysHQ/nextpnr/commit/f0859503.patch"
        "003-fix-build-with-clang-19.patch")
sha256sums=('ed14f31c68a1ec2e7dbae78ebfb3a0a1baa16d9e5c6d0252ffae6485f61906a4'
            '69f73e0ea3111f7580dc57b62bfefb0943b67de6c9245364f435abf58973d820'
            '4e93475cc6c09b93b707d64e06779f419eddcc6f705e5f568180795f0915f745'
            '8ef99dcca08c7e6b23ca709a7cc083160ef5d78b485606c617697142c31585b9')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

# pkgver() {
#   cd "${_realname}"
#   git describe --long --tags "${_commit}" | sed 's/nextpnr-//;s/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
# }

prepare() {
  cd "${srcdir}/${_realname}"
  apply_patch_with_msg \
    001-fix-find-boost-built-with-cmake.patch \
    002-fix-build-with-boost-1.85.patch \
    003-fix-build-with-clang-19.patch

  git submodule update --init --recursive --depth=1
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  unset _extra-archs
  if [ "$CARCH" = "x86_64" ]; then
    _extra_archs=";ecp5"
  fi

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -DICESTORM_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DTRELLIS_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    -DARCH="generic;ice40${_extra_archs}" \
    -DUSE_OPENMP=ON \
    -DUSE_IPO=OFF \
    -DBUILD_PYTHON=ON \
    -DBUILD_GUI=ON \
    -DBUILD_TESTS=OFF \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DPython3_LIBRARY=${MINGW_PREFIX}/lib/libpython${_pyver}.dll.a \
    -DBoost_NO_BOOST_CMAKE=OFF \
    ../${_realname}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cmake -DBUILD_TESTS=ON ../${_realname}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}/COPYING \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/COPYING
}
